Imports System.Data.SqlClient
Imports System.Collections.Generic

Partial Public Class Details
    Inherits BasePage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.User.IsInRole("Administrator") And SiteSettings.OnlyAdminsCanEdit Then
            btnDelete.Enabled = False
            btnSave.Enabled = False
        Else
            btnDelete.Enabled = True
            btnSave.Enabled = True
        End If
        If Not IsPostBack Then
            BuildVendorList()
            If Not Request.QueryString("id") Is Nothing Then
                If Request.QueryString("id") = "new" Then
                    ' NEW
                    BuildNew()
                Else
                    ' item
                    GetDetails(New Guid(Request.QueryString("id")))
                End If
            ElseIf Not Page.RouteData.Values("item") Is Nothing Then
                GetDetails(New Guid(Page.RouteData.Values("item").ToString))
            Else
                ' NEW
                BuildNew()
            End If
        End If
    End Sub

    Private Sub BuildNew()
        lblID.Text = ""
        Me.Title = "ITracker: Add Item"
        lblPageHeader.Text = "New Item Details: "
        txtDesc.Text = ""
        txtMFGNum.Text = ""
        txtNotes.Text = ""
        txtPartNum.Text = ""
        txtQTY.Text = ""
        txtReorderPt.Text = ""
        ddlVendor.SelectedIndex = 0
        btnDelete.Enabled = False
    End Sub

    Private Sub BuildVendorList()

        Dim _vendorList As Generic.List(Of Vendor) = Vendor.FetchList(True)
        ddlVendor.DataTextField = "Name"
        ddlVendor.DataValueField = "ID"

        ddlVendor.DataSource = _vendorList
        ddlVendor.DataBind()
        ddlVendor.Items.Insert(0, (New ListItem(" -- select --", Guid.Empty.ToString)))

    End Sub
    Private Sub GetDetails(ByVal id As Guid)
        Me.Title = "ITracker: Item Details"

        Dim _invItem As InventoryItem = InventoryItem.Fetch(id)

        lblPageHeader.Text = "Item Details: " & _invItem.Description
        lblID.Text = id.ToString
        txtDesc.Text = _invItem.Description
        txtMFGNum.Text = _invItem.MFGNumber
        txtNotes.Text = _invItem.Notes
        txtPartNum.Text = _invItem.PartNumber
        txtQTY.Text = _invItem.Quantity
        txtReorderPt.Text = _invItem.ReorderPoint
        ddlVendor.SelectedValue = _invItem.VendorID.ToString

        rptrHistory.DataSource = InventoryItem.GetHistory(id)
        rptrHistory.DataBind()
    End Sub

    Protected Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        lblError.Text = ""
        Dim _item As New InventoryItem
        If lblID.Text.Trim.Length > 0 Then
            _item = InventoryItem.Fetch(New Guid(lblID.Text.Trim))
        End If
        With _item
            .Description = txtDesc.Text.Trim
            .MFGNumber = txtMFGNum.Text.Trim
            .Notes = txtNotes.Text.Trim
            .PartNumber = txtPartNum.Text.Trim
            .Quantity = CInt(txtQTY.Text.Trim)
            .ReorderPoint = CInt(txtReorderPt.Text.Trim)
            .VendorID = New Guid(ddlVendor.SelectedValue)
        End With
        If InventoryItem.Save(_item) Then
            Response.Redirect(Utils.RelativeWebRoot & "Default.aspx")
        Else
            lblError.Text = "Error saving inventory item."
        End If
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Dim msgOut As String = String.Empty
            If InventoryItem.Delete(New Guid(lblID.Text.Trim), msgOut) Then
                Response.Redirect("~/")
            Else
                Throw New Exception(msgOut)
            End If
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
            lblError.Text = ex.Message
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If Not Request.QueryString("id") Is Nothing Then
            If Request.QueryString("id") = "new" Then
                ' NEW
                BuildNew()
            Else
                ' item
                GetDetails(New Guid(Request.QueryString("id")))
            End If
        ElseIf Not Page.RouteData.Values("item") Is Nothing Then
            GetDetails(New Guid(Page.RouteData.Values("item").ToString))
        Else
            ' NEW
            BuildNew()
        End If
    End Sub
End Class